package com.masterclient.app.modules.pickgoods.contract;

import com.eoner.baselib.presenter.ThrowableConsumer;
import com.eoner.baselibrary.bean.order.ConfirmOrderBean;
import com.masterclient.app.httpservice.RetrofitUtil2;
import org.json.JSONException;
import org.json.JSONObject;
import io.reactivex.Observable;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import okhttp3.RequestBody;

public class PickGoodsPresenter extends PickGoodsContract.Presenter{

    @Override
    public void confirm_order(String buy_product, String buy_type, String charge_account,String sale_type,String relation_order_id,String is_custody) {
        JSONObject jsonObject = new JSONObject();
        try {
            if (buy_product!=null) {
                JSONObject buyProduct = new JSONObject(buy_product);
                jsonObject.put("buy_product",buyProduct);
            }
            jsonObject.put("buy_type",Integer.parseInt(buy_type));
            jsonObject.put("sale_type",sale_type);
            jsonObject.put("charge_account",charge_account);
            jsonObject.put("relation_order_id",relation_order_id);
            jsonObject.put("is_custody",is_custody);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),
                jsonObject.toString());
        Observable<ConfirmOrderBean> observable = RetrofitUtil2.getInstance().confirmOrder(body);
        Disposable disposable = toSubscribe(observable, new Consumer<ConfirmOrderBean>() {
            @Override
            public void accept(@NonNull ConfirmOrderBean bean) throws Exception {
                if("0".equals(bean.getCode())){
                    mView.confirm_order_success(bean.getData());
                }else{
                    mView.confirm_order_failed(bean.getMsg());
                }
            }
        },new ThrowableConsumer(){
            @Override
            public void accept(@NonNull Throwable o) throws Exception {
                super.accept(o);
                mView.confirm_order_failed("网络异常，请重试");
            }
        });
        mRxManager.add(disposable);
    }
}